package GUI;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import Client.DBTransaction;
import DBTools.DBProcess;
import Structure.Word;

public class QueryGUI extends JFrame {

	private JPanel contentPane;
	private DBProcess dbProcess;
	private DBTransaction dbTransaction;

	JTextField queryWord;
	JTextArea answerArea;
	JTextField targedLng;
	JTextField originalLng;

	public QueryGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setLayout(new GridLayout(2, 1, 5, 5));
		setContentPane(contentPane);

		// panel for the buttons
		JPanel buttonPane = new JPanel();
		buttonPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonPane.setLayout(new GridLayout(2, 2, 5, 5));

		// panel for the three querey options
		JPanel queryPane = new JPanel();
		queryPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		queryPane.setLayout(new GridLayout(3, 1, 5, 5));

		// panel for the queries and buttons
		JPanel actionPane = new JPanel();
		actionPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		actionPane.setLayout(new GridLayout(1, 2, 5, 5));
		actionPane.add(buttonPane);		
		actionPane.add(queryPane);
		
		contentPane.add(actionPane);
		
		// buttons
		JButton jButton = new JButton("start databse");
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(dbProcess != null)
                                    dbProcess.start();
			}
		});
		buttonPane.add(jButton);

		jButton = new JButton("stop databse");
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(dbProcess != null)
                                    dbProcess.stop();
			}
		});
		buttonPane.add(jButton);

		jButton = new JButton("translate");
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				query();
			}
		});
		buttonPane.add(jButton);

		jButton = new JButton("connect");
		jButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(dbTransaction.connect()){
                                    QueryGUI.this.answerArea.setText(QueryGUI.this.answerArea.getText()+"\n"+"connected");
                                } else {
                                    QueryGUI.this.answerArea.setText(QueryGUI.this.answerArea.getText()+"\n"+"error connecting");
                                }
			}
		});
		buttonPane.add(jButton);

		// field for the user word
		queryWord = new JTextField();
		queryWord.setText("Stuhl");
		queryPane.add(queryWord);

		// field for the user word
		originalLng = new JTextField();
		originalLng.setText("Deutsch");
		queryPane.add(originalLng);

		// field for the user word
		targedLng = new JTextField();
		targedLng.setText("");
		queryPane.add(targedLng);

		// field for the translated words
		answerArea = new JTextArea();
		answerArea.setText("answer");
		answerArea.setEditable(false);
		contentPane.add(new JScrollPane(answerArea));

		// initialize classes for db actions
		dbProcess = new DBProcess();
		dbTransaction = new DBTransaction();

	}

	private void query() {
		String word = queryWord.getText().trim();
		Word[] answers = dbTransaction.findTranslations(word);
		String answerTxt = "";
		for (int i = 0; i < answers.length; i++) {
			answerTxt += answers[i].toString() + "\n";
		}

		answerArea.setText(answerTxt);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					QueryGUI frame = new QueryGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

}
